// this js contains some components for handling different business needs

// a template for creating components
/*
	a component contains three parts: head, body and foot. buttons are placed in the foot
*/
PopupComponent = function () {
	var _leftPos = 450;
	var _topPos = 100;
	var _panel = null;
	var _body = null;
	var _head = null;
	var _self = this;
	var _confirmBtn = null;
	var _shield = null;
	
	// this func will be overried by sub classes
	this.initalize = function () {
		this.init();
	};
	
	// this func should not be overried
	this.init = function () {
		_panel = createPanel();
		document.body.appendChild(_panel);
		_shield = createShield();
		document.body.appendChild(_shield);
	};
	
	//
	this.addItem = function (html) {
		addItemToBody(html);
	};
	
	//
	this.show = function () {
		_panel.style.left = (_leftPos + RDC.gui.leftPosition()) + "px";
		_panel.style.top = (_topPos + RDC.gui.topPosition()) + "px";
		_shield.style.display = "block";
		_panel.style.display = "block";
	};
		
	// 
	this.resize = function (w, h) {
		_panel.style.width = w + "px";
		_panel.style.height = h + "px";
	};
	
	//
	this.setTitle = function (title) {
		_head.innerHTML = title;
	};
	
	// 
	this.hide = function () {
		_panel.style.display = "none";
		_shield.style.display = "none";
	};
	// must return true or false
	this.handleBiz = function () {
		window.alert("Function not carried out!");
		return false;
	};
	// 
	this.hideConfirmBtn = function(yes){
		if(_confirmBtn !== "undefined" && _confirmBtn !== null){
			_confirmBtn.style.display = yes ? "block" : "none";
		}
	};
	//
	function createPanel() {
		var panel = document.createElement("div");
		panel.className = "component";
		panel.appendChild(createHead());
		_body = createBody();
		panel.appendChild(_body);
		panel.appendChild(createFoot());
		panel.style.left = (_leftPos + RDC.gui.leftPosition()) + "px";
		panel.style.top = (_topPos + RDC.gui.topPosition()) + "px";
		return panel;
	}
	function createHead() {
		var head = document.createElement("div");
		head.className = "head";
		head.innerHTML = "Add Eric to your friend?";
		_head = head;
		return head;
	}
	function createBody() {
		var body = document.createElement("ul");
		return body;
	}
	function addItemToBody(html) {
		var li = document.createElement("li");
		li.innerHTML = html;
		_body.appendChild(li);
	}
	function createFoot() {
		var foot = document.createElement("div");
		foot.className = "foot";
		
		// confirm button
		var btn = document.createElement("input");
		btn.type = "button";
		btn.className = "confirmBtn";
		btn.value = "\u786e\u5b9a";
		btn.onclick = function () {
			this.disabled = true;
			this.value = "提交中";
			dwr.engine.setAsync(false);
			flag = false;
			try {
				flag = _self.handleBiz();
			}
			catch (e) {
			}
			dwr.engine.setAsync(true);
			this.disabled = false;
			this.value = "\u786e\u5b9a";
			if(flag !== false) {
				_self.hide();
			}
		};
		foot.appendChild(btn);
		_confirmBtn = btn;
		
		// cancel button
		btn = document.createElement("input");
		btn.type = "button";
		btn.className = "cancelBtn";
		btn.value = "返回";
		btn.onclick = function () {
			_self.hide();
		};
		foot.appendChild(btn);
		return foot;
	}
	// create a mask that shields whole screen
	function createShield() {
		var div = document.createElement("div");
		div.id = "CW_screen_shield";
		div.className = "screenShield";
		div.style.width = document.body.scrollWidth + "px";
		div.style.height = document.body.scrollHeight + "px";
		// div.style.zIndex = "1000";
		return div;
	}
};

// transfer the parameter for ajax methods. 
PopupComponent.prototype.parameters = [];
//---------------------------------------------------------------------------------------

// request popup dialog
SendRequestPopup = function () {
	this.initialize();
};
SendRequestPopup.prototype = new PopupComponent();
SendRequestPopup.prototype.initialize = function () {
	this.init();
	this.setTitle("\u52a0TA\u4e3a\u597d\u53cb?");
	this.addItem("<em><strong>\u53ea\u6709TA\u540c\u610f\u60a8\u7684\u8bf7\u6c42\u540e\u624d\u80fd\u6210\u4e3a\u597d\u53cb</strong></em>");
	this.addItem("\u9644\u8a00(50\u4e2a\u5b57\u4ee5\u5185):");
	this.addItem("<textarea id='requestNote' class='input-text' rows='3' cols='40'></textarea>");
};
SendRequestPopup.prototype.setData = function (userId, userName) {
	// remove all data stored in parameters
	while (this.parameters.length > 0) {
		this.parameters.pop();
	}
	
	if(RDC.util.isString(userId) && RDC.util.isString(userName)){
		this.parameters.push(userId);
		this.setTitle("\u5c06<b>" + userName + "</b>\u52a0\u4e3a\u597d\u53cb?");
		$("requestNote").value = "";
	}
	else {
		window.alert("Invalid parameter");
	}
};
SendRequestPopup.prototype.handleBiz = function () {
	if (!RDC.util.isArray(this.parameters) || this.parameters.length < 1) {
		window.alert("Invalid parameter.");
		return false;
	}
	
	note = $("requestNote").value;
	if(!RDC.util.verifyText(note, 0, 50)) {
		PromptDialog.show("附言不超过50个字");
		$("requestNote").focus();
		return false;
	}

	var req = {};
	req.recipientId = this.parameters.pop();
	req.note = note;
	RdcAjax.createRequest(req, function (data) {
		if (data !== null) {
			PromptDialog.show("发送好友请求成功");
		}
		else {
			PromptDialog.show("发送请求失败，您可能：<br>1.尚未登录<br>2.你们已经是好友了");
		}
	});
};

//--------------------------------------------------------------------------------------
ConfirmRequestPopup = function () {
	this.initialize();
};
ConfirmRequestPopup.prototype = new PopupComponent();
ConfirmRequestPopup.prototype.initialize = function () {
	this.init();
	this.addItem("请选择一个分组:<select><option selected>暂时不能分组</option></select>");
};
	
ConfirmRequestPopup.prototype.setData = function (dispId, recpId, userName) {
	// remove all data stored in parameters
	while (this.parameters.length > 0) {
		this.parameters.pop();
	}
	
	if(RDC.util.isString(dispId) && RDC.util.isString(recpId) && RDC.util.isString(userName)){
		this.parameters.push(dispId);
		this.setTitle("同意将<b>" + userName + "</b>加为自己的好友?");
	}
	else {
		this.setTitle("未能获取合法参数");
	}
};
ConfirmRequestPopup.prototype.handleBiz = function () {
	if (!RDC.util.isArray(this.parameters) || this.parameters.length < 1) {
		window.alert("Invalid parameter.");
		return false;
	}

	dispatcherId = this.parameters.pop();
	RdcAjax.completeRequest(dispatcherId, function (data) {
		if(typeof data.id !== 'undefined'){
			rli = $("req" + data.id);
			if(rli !== null){
				RDC.gui.removeNode(rli);
				if(RDC.util.isNumber($("requestCount").innerHTML)){
					$("requestCount").innerHTML = parseInt($("requestCount").innerHTML) - 1;
				}
				PromptDialog.show("你们已经是好朋友了，去看看TA吧");
			}
			else {
				alert("invalid parameter");
			}
		}
		else {
			PromptDialog.show("未能成功处理请求，因为" + data);
		}
	});
};

//---------------------------------------------------------------------------------------
SelectGroupPopup = function () {
	this.initialize();
};
SelectGroupPopup.prototype = new PopupComponent();
SelectGroupPopup.prototype.initialize = function () {
	this.init();
	this.setTitle("为好友选择分组");
	var item = "给<strong id='select_group_fname'>TA</strong>选择分组: <select id='select_group'>";
	item += "<option selected value='-1'>其他</option></select>";
	this.addItem(item);
};
SelectGroupPopup.prototype.setData = function(friend) {
	// remove all data stored in parameters
	while (this.parameters.length > 0) {
		this.parameters.pop();
	}
	$("select_group_fname").innerHTML = friend.name;
	this.parameters.push(friend);
};

//---------------------------------------------------------------------------------------
AddGroupPopup = function() {
	this.initialize();
};
AddGroupPopup.prototype = new PopupComponent();
AddGroupPopup.prototype.initialize = function () {
	this.init();
	this.setTitle("新建分组");
	this.addItem("<em>分组名不超过8个字，备注不超过50个字</em>");
	this.addItem("新增分组:&nbsp;<input id='group_new_name' type='text' class='input-text' maxlength='8'/>");
	this.addItem("备注:<br><textarea id='group_new_note' class='input-text' style='width:220px;'></textarea>");
};
AddGroupPopup.prototype.setData = function(parent, clonedNode) {
	$("group_new_name").value = "";
	$("group_new_note").value = "";
	while (this.parameters.length > 0) {
		this.parameters.pop();
	}
	this.parameters.push(parent);
	this.parameters.push(clonedNode);
};
AddGroupPopup.prototype.handleBiz = function () {
	name = $("group_new_name").value;
	note = $("group_new_note").value;
	if(!RDC.util.verifyText(name, 1, 8)) {
		PromptDialog.show("分组的名字字数在1~8之间");
		$("group_new_name").focus();
		return false;
	}
	if(!RDC.util.verifyText(note, 0, 50)) {
		PromptDialog.show("分组备注不超过50个字");
		$("group_new_note").focus();
		return false;
	}
	
	var clonedNode = this.parameters.pop();
	var parent = this.parameters.pop();
	var group = {id:null};
	group.name = name;
	group.note = note;
	RdcAjax.addFriendGroup(group, function(data){
		if(data && typeof data !== "undefined" && typeof data.id !== "undefined") {
			PromptDialog.show("成功添加新的好友分组");
			if(clonedNode !== null) {
				node = RDC.gui.cloneNode(clonedNode, {idSuffix:data.id});
				parent.appendChild(node);
				ele = $("groupTempLink" + data.id);
				ele.href += data.id;
				ele.innerHTML = data.name;
			}
		}
		else {
			PromptDialog.show("添加好友分组失败，原因是" + data);
		}
	});
};
//---------------------------------------------------------------------------------------
RemoveGroupPopup = function() {
	this.initialize();
};
RemoveGroupPopup.prototype = new PopupComponent();
RemoveGroupPopup.prototype.initialize = function () {
	this.init();
	this.setTitle("删除分组");
	this.addItem("确定要删除该分组？<br>删除分组不会删除分组里的好友");
};
//---------------------------------------------------------------------------------------
EditGroupPopup = function() {
	this.initialize();
};
EditGroupPopup.prototype = new PopupComponent();
EditGroupPopup.prototype.initialize = function () {
	this.init();
	this.setTitle("编辑分组");
	this.addItem("<em>分组名不超过8个字，备注不超过50个字</em>");
	this.addItem("给分组另取名:&nbsp;<input id='group_edit_name' type='text' class='input-text' maxlength='8'/>");
	this.addItem("备注:<br><textarea id='group_edit_note' class='input-text' style='width:220px;'></textarea>");
};
//---------------------------------------------------------------------------------------
RemoveFriendPopup = function() {
	this.initialize();
};
RemoveFriendPopup.prototype = new PopupComponent();
RemoveFriendPopup.prototype.initialize = function () {
	this.init();
	this.setTitle("删除好友");
	this.addItem("<em>取消和<strong id='remove_friend_name_1'>TA</strong>的好友关系后，您们将不能互相访问对方的信息。</em>");
	this.addItem("您确定要和<strong id='remove_friend_name_2'>TA</strong>断绝关系？");
};
RemoveFriendPopup.prototype.setData = function(friend, htmlObj) {
	// remove all data stored in parameters
	while (this.parameters.length > 0) {
		this.parameters.pop();
	}
	$("remove_friend_name_1").innerHTML = friend.name;
	$("remove_friend_name_2").innerHTML = friend.name;
	this.parameters.push(htmlObj);
	this.parameters.push(friend);
};
RemoveFriendPopup.prototype.handleBiz = function () {
	if (!RDC.util.isArray(this.parameters) || this.parameters.length < 1) {
		window.alert("Invalid parameter.");
		return false;
	}

	var friend = this.parameters.pop();
	var htmlObj = this.parameters.pop();
	RdcAjax.removeFriend(friend.friendId, function (data) {
		if(data === true){
			PromptDialog.show("成功删除好友");
			RDC.gui.removeNode(htmlObj);
		}
		else {
			PromptDialog.show("未能删除好友，因为" + data);
		}
	});
};

// -------------------------------------------------
// create, edit and delete comments
//cmtUtil = new CommentAjax();
function CommentUtil(type, typeId, authorId, hostId) {
	var _comment = {id: null, entryType: type, typeId: typeId, authorId: authorId, hostId: hostId};
	var _editPop = new EditCommentPopup();
	var _deltPop = new DeltCommentPopup();
	var _replyPop = new ReplyCommentPopup();
	
	this.add = function() {
		elem = $("cmtContent");
		if(!RDC.util.verifyText(elem.value, 1, 200)) {
			PromptDialog.show("评论内容字数在1~200之间");
			elem.focus();
			return;
		}
		
		RDC.gui.disable("publishComment");
		reserved = $("publishComment").value;
		$("publishComment").value = "提交中";
		_comment.content = elem.value;
		RdcAjax.createComment(_comment, function(result) {
			if(typeof result.id !== "undefined") {
				PromptDialog.show("成功添加评论");
				elem.value = ""; // clean
				
				node = RDC.gui.cloneNode($("commentNode"), {idSuffix:result.id});
				$("commentList").appendChild(node);
				$("cmtTime" + result.id).innerHTML = RDC.util.dateToString(result.updatedTime, "yyyy-MM-dd HH:mm");
				$("comment_content" + result.id).innerHTML = result.content;
			}
			else {
				PromptDialog.show("添加评论失败, 由于" + result);
			}
			$("publishComment").value = reserved;
			RDC.gui.enable("publishComment");
		});
	};
	
	this.edit = function(cid) {
		_comment.id = cid;
		_comment.content = $("comment_content" + cid).innerHTML;
		_editPop.setData(_comment);
		_editPop.show();
	};
	
	this.remove = function(cid) {
		_comment.id = cid;
		_comment.content = $("comment_content" + cid).innerHTML;
		_deltPop.setData(_comment, $("commentNode" + cid));
		_deltPop.show();
	};
}
EditCommentPopup = function() {
	this.initialize();
};
EditCommentPopup.prototype = new PopupComponent();
EditCommentPopup.prototype.initialize = function () {
	this.init();
	this.setTitle("修改评论");
	this.addItem("<textarea id='modify_comment' class='input-text' style='width:350px;height:100px;'></textarea>");
};
EditCommentPopup.prototype.setData = function (comment) {
	// remove all data stored in parameters
	while (this.parameters.length > 0) {
		this.parameters.pop();
	}
	if(typeof comment.content === "undefined") {
		window.alert("invalid comment");
		return;
	}
	$("modify_comment").value = comment.content;
	this.parameters.push(comment);
};
EditCommentPopup.prototype.handleBiz = function () {
	elem = $("modify_comment");
	if(!RDC.util.verifyText(elem.value, 1, 200)) {
		PromptDialog.show("评论内容字数在1~200之间");
		elem.focus();
		return;
	}
	comment = this.parameters.pop();
	comment.content = elem.value;
	RdcAjax.editComment(comment, function(result){
		if(typeof result.id !== "undefined") {
			PromptDialog.show("成功修改评论");
			$("cmtTime" + result.id).innerHTML = RDC.util.dateToString(result.updatedTime, "yyyy-MM-dd HH:mm");
			$("comment_content" + result.id).innerHTML = result.content;
		}
		else {
			PromptDialog.show("编辑评论失败, 由于" + result);
		}
	});
};
DeltCommentPopup = function() {
	this.initialize();
};
DeltCommentPopup.prototype = new PopupComponent();
DeltCommentPopup.prototype.initialize = function () {
	this.init();
	this.setTitle("修改评论");
	this.addItem("确定要删除评论：<strong id='delete_comment'></strong>？");
};
DeltCommentPopup.prototype.setData = function (comment, htmlObj) {
	// remove all data stored in parameters
	while (this.parameters.length > 0) {
		this.parameters.pop();
	}
	if(typeof comment.id === "undefined" || htmlObj === null || typeof htmlObj === "undefined") {
		window.alert("invalid comment");
	}
	$("delete_comment").innerHTML = RDC.util.trimString(comment.content, 20);
	this.parameters.push(comment);
	this.parameters.push(htmlObj);
};
DeltCommentPopup.prototype.handleBiz = function () {
	if (!RDC.util.isArray(this.parameters) || this.parameters.length < 1) {
		window.alert("Invalid parameter.");
		return false;
	}
	htmlObj = this.parameters.pop();
	comment = this.parameters.pop();
	RdcAjax.deleteComment(comment, function(result){
		if(result === true) {
			PromptDialog.show("成功删除评论");
			RDC.gui.removeNode(htmlObj);
		}
		else {
			PromptDialog.show("删除评论失败, 由于" + result);
		}
	});
};
ReplyCommentPopup = function() {
	this.initialize();
};
ReplyCommentPopup.prototype = new PopupComponent();
ReplyCommentPopup.prototype.initialize = function () {
	this.init();
	this.setTitle("回复评论");
	this.addItem("<textarea id='reply_comment' class='input-text' style='width:350px;height:100px;'></textarea>");
};
ReplyCommentPopup.prototype.setData = function (comment) {
	// remove all data stored in parameters
	while (this.parameters.length > 0) {
		this.parameters.pop();
	}
	if(typeof comment.id === "undefined") {
		window.alert("invalid comment");
		return;
	}
	this.parameters.push(comment);
};
ReplyCommentPopup.prototype.handleBiz = function () {
	elem = $("reply_comment");
	if(!RDC.util.verifyText(elem.value, 1, 200)) {
		PromptDialog.show("评论内容字数在1~200之间");
		elem.focus();
		return false;
	}
	comment = this.parameters.pop();
	comment.content = elem.value;
	RdcAjax.createComment(_comment, function(result) {
		if(typeof result.id !== "undefined") {
			node = RDC.gui.cloneNode($("commentNode"), {idSuffix:result.id});
			$("commentList").appendChild(node);
			$("cmtTime" + result.id).innerHTML = RDC.util.dateToString(result.updatedTime, "yyyy-MM-dd HH:mm");
			$("comment_content" + result.id).innerHTML = result.content;
		}
		else {
			PromptDialog.show("添加评论失败, 由于" + result);
		}
	});
};